/**
 * 
 */
package idv.takeshi.software.productline.bookstore.application.service.spring;

import org.apache.commons.lang.Validate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import idv.takeshi.software.productline.bookstore.application.service.InvalidOrderIdException;
import idv.takeshi.software.productline.bookstore.application.service.InvalidPickingListSequenceException;
import idv.takeshi.software.productline.bookstore.application.service.MaintainPickingListException;
import idv.takeshi.software.productline.bookstore.application.service.MaintainPickingListsService;
import idv.takeshi.software.productline.bookstore.domain.model.order.Order;
import idv.takeshi.software.productline.bookstore.domain.model.order.OrderRepository;
import idv.takeshi.software.productline.bookstore.domain.model.order.PickingList;

/**
 * A implementation for service MaintainPickingListsService. 
 * @author takeshi
 *
 */
@Service("maintainPickingListsService")
@Transactional
public class MaintainPickingListsServiceImpl implements
		MaintainPickingListsService {
	
	@Autowired
	private OrderRepository orderRepository; 
	
	/* (non-Javadoc)
	 * @see idv.takeshi.software.productline.bookstore.application.service.MaintainPickingListsService#receivePickingList(java.lang.Long, int)
	 */
	public void receivePickingList(Long orderId, int pickingListSequence) throws MaintainPickingListException {
		Validate.notNull(orderId, "orerId shall always not be null");
		Validate.isTrue(pickingListSequence > 0, "pickingListSequence:" + pickingListSequence +
				" shall always greater than 0");
		Order order = this.orderRepository.findById(orderId);
		PickingList pickingList = null;
		order = this.orderRepository.findById(orderId);
		if(null == order)
			throw new InvalidOrderIdException("orderId:" + orderId + " can not find corresponding order object");
		
		pickingList = order.getActivePickingList();
		if(null == pickingList || pickingList.getSequence() != pickingListSequence)
			throw new InvalidPickingListSequenceException("pickingListSequence:" + pickingListSequence + 
					" can not find corresponding active pickingList for order: + orderId");
		
		order.arrive();
	}

}
